package org.glow.changgou.order.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glow.changgou.order.dao.OrderLogMapper;
import org.glow.changgou.order.pojo.OrderConfig;
import org.glow.changgou.order.pojo.OrderItem;
import org.glow.changgou.order.pojo.OrderLog;
import org.glow.changgou.order.service.OrderLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/****
 * @Author:glow
 * @Description:OrderLog业务层接口实现类
 *****/
@Service
public class OrderLogServiceImpl extends ServiceImpl<OrderLogMapper,OrderLog> implements OrderLogService {

    @Resource
    private OrderLogMapper orderLogMapper;


    /**
     * OrderLog条件+分页查询
     * @param orderLog 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<OrderLog> findPage(OrderLog orderLog, int page, int size){
        Page<OrderLog> pageInfo = new Page<>(page,size);
        LambdaQueryWrapper<OrderLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,orderLog);
        return orderLogMapper.selectPage(pageInfo,queryWrapper);
    }

    /**
     * OrderLog分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<OrderLog> findPage(int page, int size){
        Page<OrderLog> pageInfo = new Page<>(page,size);

        return orderLogMapper.selectPage(pageInfo,null);
    }

    /**
     * OrderLog条件查询
     * @param orderLog
     * @return
     */
    @Override
    public List<OrderLog> findList(OrderLog orderLog){

        LambdaQueryWrapper<OrderLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,orderLog);
        return orderLogMapper.selectList(queryWrapper);
    }


    /**
     * OrderLog构建查询对象
     * @param orderLog
     * @return
     */
    public LambdaQueryWrapper<OrderLog> getCondition(LambdaQueryWrapper<OrderLog> queryWrapper, OrderLog orderLog){

        if(orderLog!=null){
            // ID
            if(!StringUtils.isEmpty(orderLog.getId())){
//                    criteria.andEqualTo("id",orderLog.getId());
                    queryWrapper.eq(OrderLog::getId,orderLog.getId());
            }
            // 操作员
            if(!StringUtils.isEmpty(orderLog.getOperater())){
                queryWrapper.eq(OrderLog::getOperater,orderLog.getOperater());
//                    criteria.andEqualTo("operater",orderLog.getOperater());
            }
            // 操作时间
            if(!StringUtils.isEmpty(orderLog.getOperateTime())){
                queryWrapper.eq(OrderLog::getOperateTime,orderLog.getOperateTime());
//                    criteria.andEqualTo("operateTime",orderLog.getOperateTime());
            }
            // 订单ID
            if(!StringUtils.isEmpty(orderLog.getOrderId())){
                queryWrapper.eq(OrderLog::getOrderId,orderLog.getOrderId());
//                    criteria.andEqualTo("orderId",orderLog.getOrderId());
            }
            // 订单状态,0未完成，1已完成，2，已退货
            if(!StringUtils.isEmpty(orderLog.getOrderStatus())){
                    queryWrapper.eq(OrderLog::getOrderStatus,orderLog.getOrderStatus());
//                    criteria.andEqualTo("orderStatus",orderLog.getOrderStatus());
            }
            // 付款状态
            if(!StringUtils.isEmpty(orderLog.getPayStatus())){
//                    criteria.andEqualTo("payStatus",orderLog.getPayStatus());
                    queryWrapper.eq(OrderLog::getPayStatus,orderLog.getPayStatus());
            }
            // 发货状态
            if(!StringUtils.isEmpty(orderLog.getConsignStatus())){
//                    criteria.andEqualTo("consignStatus",orderLog.getConsignStatus());
                    queryWrapper.eq(OrderLog::getConsignStatus,orderLog.getConsignStatus());
            }
            // 备注
            if(!StringUtils.isEmpty(orderLog.getRemarks())){
//                    criteria.andEqualTo("remarks",orderLog.getRemarks());
                    queryWrapper.eq(OrderLog::getRemarks,orderLog.getRemarks());
            }
            // 支付金额
            if(!StringUtils.isEmpty(orderLog.getMoney())){
//                    criteria.andEqualTo("money",orderLog.getMoney());
                    queryWrapper.eq(OrderLog::getMoney,orderLog.getMoney());
            }
            // 
            if(!StringUtils.isEmpty(orderLog.getUsername())){

//                    criteria.andLike("username","%"+orderLog.getUsername()+"%");
                    queryWrapper.like(OrderLog::getUsername,orderLog.getUsername());
            }
        }
        return queryWrapper;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        orderLogMapper.deleteById(id);
    }

    /**
     * 修改OrderLog
     * @param orderLog
     */
    @Override
    public void update(OrderLog orderLog){
        orderLogMapper.updateById(orderLog);
    }

    /**
     * 增加OrderLog
     * @param orderLog
     */
    @Override
    public void add(OrderLog orderLog){
        orderLogMapper.insert(orderLog);
    }

    /**
     * 根据ID查询OrderLog
     * @param id
     * @return
     */
    @Override
    public OrderLog findById(String id){
        return  orderLogMapper.selectById(id);
    }

    /**
     * 查询OrderLog全部数据
     * @return
     */
    @Override
    public List<OrderLog> findAll() {
        return orderLogMapper.selectList(null);
    }
}
